#region Apache License
//
// Licensed to the Apache Software Foundation (ASF) under one or more 
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership. 
// The ASF licenses this file to you under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with 
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#endregion


using System;
using System.Collections.Generic;
using System.Text;
using IPSim.Data;
using IPSim.Core;

namespace IPSim.Devices
{
    public class Router : IConnectable
    {
        //private bool                m_powered;
        private IPInterface         m_ipInterface1;
        private IPInterface         m_ipInterface2;
        private IPInterface         m_ipInterface3;
        private IPInterface         m_ipInterface4;
        private EthernetInterface   m_ethernetInterface1;
        private EthernetInterface   m_ethernetInterface2;
        private EthernetInterface   m_ethernetInterface3;
        private EthernetInterface   m_ethernetInterface4;
        private TCPInterface        m_tcpInterface1;
        private UDPInterface        m_udpInterface1;
        private TCPInterface        m_tcpInterface2;
        private UDPInterface        m_udpInterface2;
        private TCPInterface        m_tcpInterface3;
        private UDPInterface        m_udpInterface3;
        private TCPInterface        m_tcpInterface4;
        private UDPInterface        m_udpInterface4;
        private ARPInterface        m_arpInterface;
        private RIPInterface        m_ripInterface;


        public Router(IPAddress ipAddr1, IPAddress ipAddr2, IPAddress ipAddr3, IPAddress ipAddr4, IPAddress subNet)
        {
            //m_powered               = false;
            m_ethernetInterface1    = new EthernetInterface();
            m_ethernetInterface2    = new EthernetInterface();
            m_ethernetInterface3    = new EthernetInterface();
            m_ethernetInterface4    = new EthernetInterface();
            m_ipInterface1          = new IPInterface(m_ethernetInterface1, ipAddr1);
            m_ipInterface2          = new IPInterface(m_ethernetInterface2, ipAddr2);
            m_ipInterface3          = new IPInterface(m_ethernetInterface3, ipAddr3);
            m_ipInterface4          = new IPInterface(m_ethernetInterface4, ipAddr4);
            m_arpInterface          = new ARPInterface(m_ethernetInterface1);
            m_tcpInterface1 = new TCPInterface(m_ipInterface1);
            m_udpInterface1 = new UDPInterface(m_ipInterface1);
            m_tcpInterface2 = new TCPInterface(m_ipInterface2);
            m_udpInterface2 = new UDPInterface(m_ipInterface2);
            m_tcpInterface3 = new TCPInterface(m_ipInterface3);
            m_udpInterface3 = new UDPInterface(m_ipInterface3);
            m_tcpInterface4 = new TCPInterface(m_ipInterface4);
            m_udpInterface4 = new UDPInterface(m_ipInterface4);
            m_ripInterface = new RIPInterface(m_udpInterface1, m_udpInterface2, m_udpInterface3, m_udpInterface4);
        }

        public void SendUDPPacket(UDPPacket udpPacket, IPAddress destIP)
        {
            m_udpInterface1.SendUDPPacket(udpPacket, destIP);
            m_udpInterface2.SendUDPPacket(udpPacket, destIP);
            m_udpInterface3.SendUDPPacket(udpPacket, destIP);
            m_udpInterface4.SendUDPPacket(udpPacket, destIP);
        }

        public void SendTCPPacket(TCPPacket tcpPacket, IPAddress destIP)
        {
            m_tcpInterface1.SendTCPPacket(tcpPacket, destIP);
            m_tcpInterface2.SendTCPPacket(tcpPacket, destIP);
            m_tcpInterface3.SendTCPPacket(tcpPacket, destIP);
            m_tcpInterface4.SendTCPPacket(tcpPacket, destIP);
        }


        #region IConnectable Members

        public void Connect(IConnectable device, byte num)
        {
            switch (num)
            {
                case 1:
                    m_ethernetInterface1.Connect(device, 1);
                    break;
                case 2:
                    m_ethernetInterface2.Connect(device, 1);
                    break;
                case 3:
                    m_ethernetInterface3.Connect(device, 1);
                    break;
                case 4:
                    m_ethernetInterface4.Connect(device, 1);
                    break;
                default:
                    break;
            }

        }

        public void Disconnect(IConnectable device, byte num)
        {
            switch (num)
            {
                case 1:
                    m_ethernetInterface1.Disconnect(device, 1);
                    break;
                case 2:
                    m_ethernetInterface2.Disconnect(device, 1);
                    break;
                case 3:
                    m_ethernetInterface3.Disconnect(device, 1);
                    break;
                case 4:
                    m_ethernetInterface4.Disconnect(device, 1);
                    break;
                default:
                    break;
            }
        }

        public TransportEthernetFrame FrameReceiver1
        {
            get { return m_ethernetInterface1.FrameReceiver1; }
        }
        public TransportEthernetFrame FrameReceiver2
        {
            get { return m_ethernetInterface2.FrameReceiver1; }
        }
        public TransportEthernetFrame FrameReceiver3
        {
            get { return m_ethernetInterface3.FrameReceiver1; }
        }
        public TransportEthernetFrame FrameReceiver4
        {
            get { return m_ethernetInterface4.FrameReceiver1; }
        }

        #endregion
    }
}
